﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Shared.Model;

namespace ManageSystem.Manager
{
    public partial class FormBobData : UserControl
    {

        private List<BOB_TYPE> _listTypes;
        private BOB_TYPE _choiceType;
        private List<BOB_DATA> _listData=new List<BOB_DATA>();
        private BOB_DATA _choiceData;
        
        public FormBobData()
        {
            InitializeComponent();
        }

        private void FormBobData_Load(object sender, EventArgs e)
        {
            this.listViewBobTypes.Items.Clear();

            var types = Client.MethodWcf.SelectJsonClient<List<BOB_TYPE>>("SELECT * FROM Modem.dbo.BOB_TYPE ORDER BY COMPANY,PON_TYPE");
            if (types == null || types.Count == 0)
            {
                MessageBox.Show("未能加载BOSA类型信息");
                return;
            }

            _listTypes = types;

            for(int i = 0; i < _listTypes.Count; i++)
            {
                AddListViewItem(_listTypes[i]);
            }
        }

        private void AddListViewItem(BOB_TYPE type)
        {
            //throw new NotImplementedException();
            if (type != null)
            {
                
                ListViewItem item = new ListViewItem
                {
                    Text = type.ID.ToString()
                };

                item.SubItems.Add(type.COMPANY);
                item.SubItems.Add(type.PON_TYPE);
                item.SubItems.Add(type.BG_MIN.ToString());
                item.SubItems.Add(type.BG_MAX.ToString());
                item.SubItems.Add(type.POWER_MIN.ToString());
                item.SubItems.Add(type.POWER_MAX.ToString());

                this.listViewBobTypes.Items.Add(item);
            }
        }

        private void AddListViewItem(BOB_DATA data)
        {
            if (data == null) return;

            ListViewItem item = new ListViewItem
            {
                Text = data.TYPE_ID.ToString()
            };

            item.SubItems.Add(data.TABLE_ID.ToString());
            item.SubItems.Add(data.INDEX_START.ToString());
            item.SubItems.Add(data.INDEX_END.ToString());
            item.SubItems.Add(data.CONTEXT);

            this.listViewBobData.Items.Add(item);
        }

        private void ShowBobData(BOB_DATA data)
        {
            if (data == null) return;

            this.textBoxShowBosaId.Text = data.TYPE_ID.ToString();
            this.textBoxShowIndexStart.Text = data.INDEX_START.ToString();
            this.textBoxShowIndexEnd.Text = data.INDEX_END.ToString();
            this.textBoxShowRegisterId.Text = data.TABLE_ID.ToString();
            this.richTextBoxShowContext.Text = data.CONTEXT;
        }

        private void ListViewBobTypes_MouseClick(object sender, MouseEventArgs e)
        {
            ClearShowControls();
            ClearAddControls();
            this.listViewBobData.Items.Clear();
            _choiceType = null;
            _listData.Clear();

            //var choiceItem = this.listViewBobTypes.SelectedItems[0].Text;
            int choiceIndex = this.listViewBobTypes.SelectedIndices[0];

            //BOB_TYPE type = _listTypes.Where(a => a.ID.ToString() == choiceItem).FirstOrDefault();
            BOB_TYPE type = _listTypes[choiceIndex];

            if (type != null)
            {
                _choiceType = type;
                this.textBoxAddBosaType.Text = type.ID.ToString();
                List<BOB_DATA> datas = Client.MethodWcf.SelectJsonClient<List<BOB_DATA>>($"SELECT * FROM Modem.dbo.BOB_DATA WHERE TYPE_ID={type.ID}");
                for(int i = 0; datas != null && datas.Count > 0 && i < datas.Count; i++)
                {
                    AddListViewItem(datas[i]);
                    _listData.Add(datas[i]);
                }
            }
        }

        private void ClearShowControls()
        {
            //this.listViewBobData.Items.Clear();
            this.textBoxShowBosaId.Clear();
            this.textBoxShowIndexEnd.Clear();
            this.textBoxShowIndexStart.Clear();
            this.textBoxShowRegisterId.Clear();
            this.richTextBoxAddBobContext.Clear();
        }

        private void ClearAddControls()
        {
            this.textBoxAddBosaType.Clear();
            this.textBoxAddIndexEnd.Clear();
            this.textBoxAddIndexStart.Clear();
            this.textBoxAddRegisterId.Clear();
            this.richTextBoxShowContext.Clear();
        }

        private void ListViewBobData_MouseClick(object sender, MouseEventArgs e)
        {
            ClearShowControls();

            var choiceIndex = this.listViewBobData.SelectedIndices[0];
            _choiceData = _listData[choiceIndex];
            ShowBobData(_listData[choiceIndex]);
        }

        private void ButtonAdd_Click(object sender, EventArgs e)
        {
            if (_choiceType == null) return;

            try
            {

                BOB_DATA bobData = new BOB_DATA();
                bobData.SetUpdate();

                bobData.TYPE_ID = _choiceType.ID;
                bobData.CONTEXT = this.richTextBoxAddBobContext.Text.Trim();
                bobData.TABLE_ID = int.Parse(this.textBoxAddRegisterId.Text.Trim());
                bobData. INDEX_START = int.Parse(this.textBoxAddIndexStart.Text.Trim(), System.Globalization.NumberStyles.HexNumber);
                bobData.INDEX_END = int.Parse(this.textBoxAddIndexEnd.Text.Trim(), System.Globalization.NumberStyles.HexNumber);

                int insert = Client.MethodWcf.Insert(bobData);

                if (insert <= 0)
                {
                    MessageBox.Show("写入失败");
                }
                else
                {
                    MessageBox.Show("写入成功");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

            ClearAddControls();
            ListViewBobTypes_MouseClick(null, null);
        }

        private void ButtonDelete_Click(object sender, EventArgs e)
        {
            if (_choiceData == null) return;

            BOB_DATA dataW = new BOB_DATA();
            dataW.SetWhere();
            dataW.TYPE_ID = _choiceData.TYPE_ID;
            dataW.TABLE_ID = _choiceData.TABLE_ID;

            int delete = Client.MethodWcf.Delete(dataW);

            if (delete <= 0)
            {
                MessageBox.Show("删除失败");
            }
            else
            {
                MessageBox.Show("删除成功");
            }

            //ClearShowControls();
            ListViewBobTypes_MouseClick(null, null);
        }
    }
}
